//
//  FCPNowPlayingButton.swift
//  ad_tme_plugin
//
//  Created by 曹镭 on 2021/10/18.
//

import CarPlay

@available(iOS 14.0, *)
class FCPNowPlayingButton {

    private(set) var elementId: String
    private var type: String?
    private var image: String?
    private var completeHandler: (() -> Void)?
    
    init(obj: [String : Any]) {
        self.elementId = obj["_elementId"] as! String
        self.type = obj["type"] as? String
        self.image = obj["image"] as? String
    }

    var get: CPNowPlayingButton {
        let onPress: ((CPNowPlayingButton) -> Void) = {_ in
            DispatchQueue.main.async {
              FCPStreamHandlerPlugin.sendEvent(type: FCPChannelTypes.onNowPlayingBtnPressed, data: ["elementId": self.elementId])
            }
          }
        var playBtn = CPNowPlayingButton.init(handler: onPress)
        switch(type){
        case FCPNowPlayingButtonTypes.CPNowPlayingButton:
            playBtn = CPNowPlayingButton.init(handler: onPress)
            break
        case FCPNowPlayingButtonTypes.CPNowPlayingImageButton:
            if(image == nil){
              playBtn = CPNowPlayingButton.init(handler: onPress)
            }else {
              playBtn = CPNowPlayingImageButton.init(image: UIImage().getIcon(name: image!), handler: onPress)
            }
            break
        case FCPNowPlayingButtonTypes.CPNowPlayingAddToLibraryButton:
            playBtn = CPNowPlayingAddToLibraryButton.init(handler: onPress)
            break
        case FCPNowPlayingButtonTypes.CPNowPlayingMoreButton:
            playBtn = CPNowPlayingMoreButton.init(handler: onPress)
            break
        case FCPNowPlayingButtonTypes.CPNowPlayingPlaybackRateButton:
            playBtn = CPNowPlayingPlaybackRateButton.init(handler: onPress)
            break
        case FCPNowPlayingButtonTypes.CPNowPlayingRepeatButton:
            playBtn = CPNowPlayingRepeatButton.init(handler: onPress)
            break
        case FCPNowPlayingButtonTypes.CPNowPlayingShuffleButton:
            playBtn = CPNowPlayingShuffleButton.init(handler: onPress)
            break
        default:
            break
        }
        return playBtn
    }
}

